package com.ruoyi.web.controller;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.TopicalType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.BTopical;
import com.ruoyi.system.domain.BTopicalPraise;
import com.ruoyi.system.domain.request.CaseRequest;
import com.ruoyi.system.domain.request.TopicalRequest;
import com.ruoyi.system.domain.vo.BCaseVo;
import com.ruoyi.system.domain.vo.BTaskVo;
import com.ruoyi.system.domain.vo.BTopicalVo;
import com.ruoyi.system.service.BTopicalPraiseService;
import com.ruoyi.system.service.BTopicalService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/topical")
public class TopicalController extends BaseController {

    @Autowired
    BTopicalService topicalService;

    @Autowired
    BTopicalPraiseService topicalPraiseService;

    /**
     * 分页获取主题列表
     */
    @GetMapping("/list")
    public TableDataInfo queryList(TopicalRequest request){
        request.setUserId(SecurityUtils.getUserId());
        startPage();
        List<BTopicalVo> list = topicalService.queryList(request);
        return getDataTable(list);
    }

    /**
     * 创建主题
     * @param request
     * @return
     */
    @PostMapping("/create")
    public AjaxResult create(@Validated @RequestBody TopicalRequest request){
        BTopical topical = new BTopical();
        BeanUtils.copyProperties(request,topical);
        topical.setUserId(SecurityUtils.getUserId());
        topical.setCreateTime(new Date());
        topical.setType(TopicalType.CREATE.getValue());
        topicalService.save(topical);

        return success();
    }

    /**
     * 回复主题
     * @param request
     * @return
     */
    @PostMapping("/reply")
    public AjaxResult reply(@Validated @RequestBody TopicalRequest request){
        BTopical topical = topicalService.getById(request.getTopicalId());
        if(ObjectUtils.isEmpty(topical)){
            throw new ServiceException("该主题不存在!");
        }
        BTopical reply = new BTopical();
        BeanUtils.copyProperties(request,reply);
        reply.setUserId(SecurityUtils.getUserId());
        reply.setCaseId(topical.getCaseId());
        reply.setParentId(topical.getId());
        reply.setCreateTime(new Date());
        reply.setType(TopicalType.REPLY.getValue());
        topicalService.save(reply);

        //更新主题回复数
        topical.setReplyNum(topical.getReplyNum()+1);
        topicalService.updateById(topical);
        Map result = MapUtil.builder().put("id",topical.getId()).put("replyNum",topical.getReplyNum()).build();
        return success(result);
    }

    /**
     * 主题点赞
     */
    @GetMapping(value = "/praise/{topicalId}")
    public AjaxResult praise(@PathVariable Long topicalId) {
        Long userId = SecurityUtils.getUserId();
        BTopical topical = topicalService.getById(topicalId);
        if(ObjectUtils.isEmpty(topical)){
            throw new ServiceException("该主题不存在!");
        }
        BTopicalPraise topicalPraise = topicalPraiseService.getOne(new QueryWrapper<BTopicalPraise>().lambda()
                        .eq(BTopicalPraise::getTopicalId,topicalId)
                        .eq(BTopicalPraise::getUserId, userId));
        if(ObjectUtils.isEmpty(topicalPraise)){
            topicalPraise = new BTopicalPraise();
            topicalPraise.setTopicalId(topicalId);
            topicalPraise.setUserId(userId);
            topicalPraise.setCreateTime(new Date());
            topicalPraiseService.save(topicalPraise);
        }else{
            topicalPraiseService.removeById(topicalPraise.getId());
        }
        //更新主题点赞数
        int praiseNum = topicalPraiseService.count(new QueryWrapper<BTopicalPraise>().lambda().eq(BTopicalPraise::getTopicalId,topicalId));
        topical.setPraiseNum(praiseNum);
        topicalService.updateById(topical);

        Map result = MapUtil.builder().put("id",topicalId).put("praiseNum",praiseNum).build();
        return success(result);
    }
}
